<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Points Model
*
* Author: John Schop, john.schop@gmail.com
*
*/

class Points_model extends CI_Model
{
	

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	

	/**
	 * Get all Points
	 *
	 * @access public
	 * @return object
	 */
	function get_all()
	{
		$this->db->order_by('users.id', 'asc');
        $this->db->order_by('points.points_stage_id', 'asc');
        $this->db->join('users', 'users.id = points.points_user_id');
		return $this->db->get('points')->result();
	}

    /**
     * Get Points by stage_id
     *
     * @access public
     * @param string $stage_id
     * @return object result object
     */
    function get_by_stage_id($stage_id)
    {
        $this->db->order_by('points.points_total', 'desc');
        $this->db->join('users', 'users.id = points.points_user_id');
        return $this->db->get_where('points', array('points_stage_id' => $stage_id))->result();
    }

    /**
     * Get Points by user_id
     *
     * @access public
     * @param string $user_id
     * @return object result object
     */
    function get_by_user_id($user_id)
    {
        return $this->db->get_where('points', array('points_user_id' => $user_id))->result();
    }

    /**
     * Get Points by user_id, stage_id
     *
     * @access public
     * @param string user_id, stage_id
     * @return object result object
     */
    function get_by_user_id_stage_id($user_id, $stage_id)
    {
        $this->db->where('points_user_id', $user_id);
        $this->db->where('points_stage_id', $stage_id);
        return $this->db->get('points')->row();
    }        

    /**
     * Update pOints
     *
     * @access public
     * @param int $stage_id
     * @param array $attributes
     * @return void
     */
    function update($user_id, $stage_id, $attributes = array())
    {

        // Update
        if ($this->get_by_user_id_stage_id($user_id, $stage_id))
        {
            $this->db->where('points_user_id', $user_id);
            $this->db->where('points_stage_id', $stage_id);
            $this->db->update('points', $attributes);
        }
        // Insert
        else
        {
            $attributes['points_user_id'] = $user_id;
            $attributes['points_stage_id'] = $stage_id;
            $this->db->insert('points', $attributes);
        }

    }

    function get_top_10($stage_id)
    {
        $this->db->order_by('points_total', 'desc');
        $this->db->limit(10);
        $this->db->join('users', 'users.id = points.points_user_id');
        $this->db->join('selections', 'selections.user_id = points.points_user_id');
        return $this->db->get_where('points', array('points_stage_id'=>$stage_id))->result();
    }

    function get_top_10_overall()
    {
        
        $this->db->join('users', 'users.id = points.points_user_id');
        $this->db->join('selections', 'selections.user_id = points.points_user_id');
        $this->db->group_by('points_user_id');
        $this->db->limit(10);
        $this->db->select('users.*, selections.*');
        $this->db->select_sum('points_total', 'points_overall');
        $this->db->order_by('points_overall', 'desc');
        return $this->db->get('points')->result(); 
    }

    function get_stage_ranking($stage_id)
    {
        
        $this->db->join('users', 'users.id = points.points_user_id');
        $this->db->join('selections', 'selections.user_id = points.points_user_id');
        $this->db->group_by('points_user_id');
        $this->db->select('users.*, selections.*');
        $this->db->select_sum('points_total', 'points_overall');
        $this->db->order_by('points_overall', 'desc');
        $this->db->where('points_stage_id', $stage_id);
        return $this->db->get('points')->result(); 
    }  

    function get_overall_ranking()
    {
        
        $this->db->join('users', 'users.id = points.points_user_id');
        $this->db->join('selections', 'selections.user_id = points.points_user_id');
        $this->db->group_by('points_user_id');
        $this->db->select('users.*, selections.*');
        $this->db->select_sum('points_stage');
        $this->db->select_sum('points_yellow');
        $this->db->select_sum('points_green');
        $this->db->select_sum('points_mountain');
        $this->db->select_sum('points_total', 'points_overall');
        $this->db->order_by('points_overall', 'desc');
        return $this->db->get('points')->result(); 
    }    

	/**
	 * Delete points for user_id
	 *
	 * @access public
	 * @param int $user_id
	 * @return void
	 */
    function delete_user($user_id)
    {
    	$this->db->delete('points', array('result_user_id' => $user_id)); 
    }

    /**
     * Delete points for stage_id
     *
     * @access public
     * @param int $stage_id
     * @return void
     */
    function delete_stage($stage_id)
    {
        $this->db->delete('points', array('result_stage_id' => $stage_id)); 
    }

    function delete_all()
    {
        $this->db->empty_table('points');
    }

}
